package com.ibeacon.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ibeacon.controller.common.Log;
import com.ibeacon.controller.common.MessageManager;
import com.ibeacon.controller.common.TokenThread;
import com.ibeacon.service.IbeaconDeviceService;

import net.sf.json.JSONObject;

@Controller
public class SurroundingIbeaconController {
	Log log = Log.getLogger();
	
	@Autowired
	IbeaconDeviceService ibeaconDeviceService;
	
	
	
	@RequestMapping("/surroundingIbeacon")
	@ResponseBody
	public Map<String, String> surroundingIbeacon(ModelMap model ,HttpServletResponse response,HttpServletRequest request){
		
		String accessToken = TokenThread.accessToken.getToken();
		String appid =  TokenThread.appid;
		String appsecret =  TokenThread.appsecret;
		String ticket = TokenThread.accessToken.getTicket();
		String url = request.getParameter("url");	
		log.logger.info("weixin accessToken:"+accessToken); 
        log.logger.info("weixin api appid:"+appid);    
        log.logger.info("weixin api appsecret:"+appsecret);    
        log.logger.info("weixin api ticket:"+ticket); 
		Map<String, String> ret = ibeaconDeviceService.getSign(ticket,url);
		ret.put("appid", appid);
		return ret;
		
	}

	@RequestMapping("/getsurroundingSite")
	public String getsurroundingSite(ModelMap model ,HttpServletResponse response,HttpServletRequest request){
//		String argv = request.getParameter("arg");
		String argv="{'beacons':[{'proximity':'0','rssi':'-75.0','uuid':'FDA50693-A4E2-4FB1-AFCF-C6EB07647825','accuracy':'2.990653625360724','minor':'37933','heading':'299.62','major':'10001'},{'proximity':'0','rssi':'-74.0','uuid':'FDA50693-A4E2-4FB1-AFCF-C6EB07647825','accuracy':'2.685571651275901','minor':'37931','heading':'300.18','major':'10001'},{'proximity':'0','rssi':'-73.0','uuid':'FDA50693-A4E2-4FB1-AFCF-C6EB07647825','accuracy':'2.4152318274032263','minor':'37932','heading':'300.19','major':'10001'}],'errMsg':'onSearchBeacons:ok'}";

		JSONObject jodata = new JSONObject();
		if(argv!=null){
			jodata =  JSONObject.fromObject(argv);
		}
		log.logger.info("++++++++++++++++++++++++++++++++++++++++++++++++++++");
		log.logger.info(argv);
		List<Object>  beacons= (List<Object>) jodata.get("beacons");
		log.logger.info(beacons);
		model.put("ifsearch","search");
		model.put("sites",beacons);
		return "ibeacon/ibeaconDevice";
	}
	

}
